package com.example.day01.ptoto_wall;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;

import com.example.day01.R;
import com.squareup.picasso.Picasso;

public class PhotoAdapter extends BaseAdapter {
    Context context;
    String[]mDatas;

    public PhotoAdapter(Context context, String[] mDatas) {
        this.context = context;
        this.mDatas = mDatas;
    }
    //   返回条目的数量
    @Override
    public int getCount() {
        return mDatas.length;
    }

    @Override
    public Object getItem(int position) {
        return mDatas[position];
    }

    @Override
    public long getItemId(int position) {
        return position;
    }
    //  返回每一个item生成view对象
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.item_photowall,null);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        }else {
            holder = (ViewHolder) convertView.getTag();
        }
//        给每一个item当中的控件进行设置
        String url = mDatas[position];
//        使用Picasso加载网络图片
        Picasso.with(context).load(url).into(holder.iv);
        return convertView;
    }
    // 将item对应的视图中所有的控件都进行初始化的操作，查找控件
    class ViewHolder{
        ImageView iv;
        public ViewHolder(View view){
            iv = view.findViewById(R.id.item_photowall_iv);
        }
    }
}